<?php
/**
 * HouseSeco controller
 */
namespace App\Http\ControllersAdmin;

use App\Service\HouseSecoService;
use App\Validation\HouseSecoValidation;
use App\Validation\HouseSearchValidation;
use App\Validation\DeleteValidation;
use App\Validation\BatchValidation;

class HouseSecoController extends BaseController
{
	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new HouseSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
        $this->backgroudDateFilterByCityRole($raw);
		$data = HouseSecoService::getSearch($raw);
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house_seco . $value->id;
			$d[$key]->area = $value->area ? $value->area . "㎡" : "面积未知";
			$d[$key]->price = $value->price ? $value->price . "元/㎡" : "单价未知";
			$d[$key]->total_price = $value->total_price ? $value->total_price . "万" : "总价未知";
		}		
		$count = HouseSecoService::count($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [recoAction 最新推荐列表]
	 * @return [type] [description]
	 */
	public function recoAction()
	{
		$this->getUser();
		$raw = $this->validation(new HouseSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$raw->reco_by = 2;
		$data = HouseSecoService::getSearch($raw);	
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house_seco . $value->id;
		}	
		$count = HouseSecoService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [firstAction 首页列表]
	 * @return [type] [description]
	 */
	public function firstAction()
	{
		$this->getUser();
		$raw = $this->validation(new HouseSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$raw->index_by = 2;
		$data = HouseSecoService::getSearch($raw);	
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house_seco . $value->id;
		}
		$count = HouseSecoService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [createAction 创建]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$this->getUser();
		$raw = $this->validation(new HouseSecoValidation, $this->request->getJsonRawBody());
		$raw->id = NULL;
		if ($raw->pics && is_array($raw->pics) && count($raw->pics) > 0){
			$raw->pics = implode(',', $raw->pics);
		}else{
			$raw->pics = "";
		}
		if ($raw->reco_ids && is_array($raw->reco_ids) && count($raw->reco_ids) > 0){
			$raw->reco_ids = implode(',', $raw->reco_ids);
		}else{
			$raw->reco_ids = "";
		}

		$data = HouseSecoService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = HouseSecoService::getModel($raw->id);
		
		$arr = new \stdClass();
		//$go_url = $this->config->gourl . $this->config->urls->HouseSeco . $data->id;
        if ($data){
			$arr = new \stdClass();
			foreach ($data as $key => $value) {
				$arr->$key = $value;
			}
			if ($data->pics){
				$arr->pics = explode(",", $data->pics);	
			}else{
				$arr->pics = [];
			}
			$arr->three_url = $data->three_url ? $data->three_url : "";
			$arr->reco_ids = HouseSecoService::getRecoInfo($raw->id);
		}

		return $this->echoJson($arr);
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$raw = $this->validation(new HouseSecoValidation, $this->request->getJsonRawBody());

		if ($raw->pics && is_array($raw->pics) && count($raw->pics) > 0){
			$raw->pics = implode(',', $raw->pics);
		}else{
			$raw->pics = "";
		}
		if ($raw->reco_ids && is_array($raw->reco_ids) && count($raw->reco_ids) > 0){
			$raw->reco_ids = implode(',', $raw->reco_ids);
		}else{
			$raw->reco_ids = "";
		}

		$data = HouseSecoService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [setsouseAction 推荐到为您推荐]
	 * @return [type] [description]
	 */
	public function setyouAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		HouseSecoService::houseReco($raw->id);
		return $this->echoJson("ok");
	}

	/**
	 * [setrecoAction 执行推荐  将HouseSeco数据列表推荐到首页-推荐]
	 * @return [type] [description]
	 */
	public function setrecoAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		HouseSecoService::batchReco($raw->id, 2);
		return $this->echoJson("ok");
	}

	/**
	 * [delrecoAction 执行取消推荐]
	 * @return [type] [description]
	 */
	public function delrecoAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		HouseSecoService::batchReco($raw->id);
		return $this->echoJson("ok");
	}

	/**
	 * [setrecoAction 执行首页 将HouseSeco数据列表推荐到首页-房源]
	 * @return [type] [description]
	 */
	public function setfirstAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		HouseSecoService::batchReco($raw->id, 1);
		return $this->echoJson("ok");
	}

	/**
	 * [stickAction 置顶]
	 * @return [type] [description]
	 */
	public function stickAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		HouseSecoService::getStick($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [upAction 上架]
	 * @return [type] [description]
	 */
	public function displayAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = HouseSecoService::display($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = HouseSecoService::delete($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [foreverAction 彻底删除]
	 * @return [type] [description]
	 */
	public function foreverAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = HouseSecoService::forever($raw->id);
		return $this->echoJson('ok');
	}
}